package com.yjc.drygoods.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.yjc.drygoods.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000120601848";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCb114Aex6ZDxmdh+Bvb2U1r5sfvSbagW5+bbqU+W08w9X4jdIwMvy6W3rBHzh9G1HYg4L2vaFP1TfQaM0vubMLBNM3oFSm2XAkT61Pd9IY3GFloG+3bT5jiFBj8EEUEB3P6kfbfY6ITtxJQhnsx7eoWD3iNNRs4ZVN71CSxi9oVgzOAgQtmQNoF0o/p4U0e5LYYJJ2AaX+BwV2X4/26xUsxAcmheFMVFbFEXHv+niJa+MWSxF6rZoXuL2oOHHJ1z5pB96YynChGUWQdKDEStOUeeIKN+BlplQpIA19ZPf1Jqi1QNdUSz4Fz1XBtpLK1M8SZZyxq18IV6tZtz6FI/dpAgMBAAECggEAP8o2VigPbEN8DrlxquuEGCebbbB1WPemDOZJe3Vlu3vF+AnFQal9PaL4alinPL6UD/Y1A163yv/R6uMbKi/vE9GTy687N1jFyHc8z8ySGU8MJxc243Q6F2/SyDWtzdKTAWo6o6OPVkhBb1M5XWyaxrPoENHjKfZksRQGfS3WdfQvBgay3nfLTFAR69jL9HNpKTrtXZGiJqriPHiMTCQzRBdho7kNmjomyq62iG3TbTNPvSwJeGGFvp+0bR9ZXbdIpr1xCD+mMzHUmDN4KmtoaGDeGQS1qvdVwmkJot+Bw11/paLLoVWY00GzjSV6i3YtO4AwM2+T8nuZ3ey1SyUGSQKBgQDZq9UiaEK0RqUjhGJac0PVoglZeFDoq5E1c4aQw3swAf+9oDiF9gwxlZmSuKgeVWb0yF8nLkTSrLgDWroYM3q9CwvZZ9hUpRQL2mKQXMJo59K8Wx9CAK+5eTSskucwKvkbWWgMduzKZ2qglp+gFqBCk/wX4vphv10G2/ETnsDRjwKBgQC3SF51bYQsxl+sgX149QXozGmsTbo1dyIwNeUltbaOnAREExe7TuD51U+WxiN1o5a4NcyNBXn0hEclGZATflhSbzy+McKK9hWaa2L3SdHyb1YuFTeqP/cTEJUNZYv0ibtQI6dPPmPooGlWfm2ez+lEY95/3iJPArV4wDTzMH67hwKBgQC1Yq9U4LTVLRlno2WiIfMhDsnjuJMTkASNGhvEfmEk/EV0axMu7n3yw8sZrK7LuDb3PBGuwKuxMySUBwKUMDHTXmUdYZ+vyXAPAIip6+JFMQk21GgTxCaq1AnE1RW52dWDqNk/qcpgUBVWhK235aLeWBr2fLpQwfpvQNB/nQbomQKBgQChg4PCJa15q6L6KO6CXiJEAYcKEUEMBp05MPVrItPqnnBf8RdTt/IoF6NYcekjrPKQCNDmYTIUmVD8z6cYlpmMo3w6+yy+OpfVCdcfpoz+AMFIQOhZ/p5jzZQcRyVrbMiFmbXB7nAqutm5IBbIAd5Fp2CW/BDt4aw76d1/hXfZFwKBgQCUn9Q1HSjY5Z0uRzDebxhIPbg51HcIiTX5/a/t+AnGvtIZ7W6VKSfabRdvqR12rUHvjAiGHYqDc8947lARnam8K71vFI9494ykMvlb3f/5EIi+cfdFgdq/v5CX645Krvvj5Hg7BcYmsnhTpxJo3XdkgZp0lW+zTGapY2If1UASUA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArewfCfT4ZbmPjod3FlNxOWq0S2cDLd1Yzs8cWlwxLTXyMe5Kuai9TWH5XE7qCqg3QK67sYwgbcANyNo2HeNMhldV2CJAcFMXbTi3BeLtR+v6U3ybr5LjF1h6KpXd9XhFthueb+aQRt/SwK2sK6Astr9OcR0j+/ysM7bPk9fCGpXgHBqAZmNrCiiUHoGoE+Ym22As6fI2Shb0uKUskx4YxTsv336hhIFmPGL62B2vwt77bZ0znKMP23eyH6cc/mxknW4dDfcjNLTL49n88l/eljUUX9JuTStdRnvVhxANvD5eCsJhOyH/40E0e2GfJjTra9M1pf56PLCyn1+TtcLk+wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://yjc-tourism.nat300.top/alipay/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://member.drygoods.top/orderList.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    //超时时间
    private String timeOut = "30m";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
